package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.CourseOrder;
import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.CourseOrderMapper;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.qo.QueryObject;
import cn.wolfcode.crm.service.ICourseOrderService;
import cn.wolfcode.crm.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CourseOrderServiceImpl implements ICourseOrderService {

    @Autowired
    private CourseOrderMapper courseOrderMapper;
    @Autowired
    private CustomerMapper customerMapper;

    public void delete(Long id) {
        courseOrderMapper.deleteByPrimaryKey(id);
    }

    public void save(CourseOrder courseOrder) {

        //设置录入时间
        courseOrder.setInputTime(new Date());
        //设置当前客户为正式客户状态
        customerMapper.changeStatus(Customer.STATUS_FORMAL, courseOrder.getCustomer().getId());

        courseOrderMapper.insert(courseOrder);
    }

    public CourseOrder get(Long id) {
        return courseOrderMapper.selectByPrimaryKey(id);
    }

    public List<CourseOrder> listAll() {
        return courseOrderMapper.selectAll();
    }

    public void update(CourseOrder courseOrder) {
        //设置录入时间
        courseOrder.setInputTime(new Date());
        //设置当前客户为正式客户状态
        customerMapper.changeStatus(Customer.STATUS_FORMAL, courseOrder.getCustomer().getId());
        courseOrderMapper.updateByPrimaryKey(courseOrder);
    }

    @Override
    public PageInfo<CourseOrder> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<CourseOrder> courseOrders = courseOrderMapper.query(qo);
        return new PageInfo(courseOrders);
    }

}
